/**
 * @project name:crm
 */
package com.yunbo.crm.service.impl;

import com.yunbo.crm.base.BaseServiceImpl;
import com.yunbo.crm.config.AppConfig;
import com.yunbo.crm.dao.SysConfigMapper;
import com.yunbo.crm.pojo.SysConfig;
import com.yunbo.crm.service.SysConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/25 12:34
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfig,Integer> implements SysConfigService {

    @Resource
    private SysConfigMapper sysConfigMapper;

    @Override
    @PostConstruct
    public void initData() {
        List<SysConfig> sysConfigList = sysConfigMapper.selectByParams(null);
        if (null == sysConfigList || sysConfigList.size() == 0) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setActiveFlag(1);
            sysConfig.setCreateBy("SYs");
            sysConfig.setCreateDate(new Date());
            sysConfigMapper.insertSelective(sysConfig);
        }
        //initAppConfig();
    }

    @Override
    public int saveOrUpdateSysConfig(SysConfig sysConfig, String updateBy) {
        return 0;
    }

    @Override
    public SysConfig findDefaultSysConfig() {
        List<SysConfig> sysConfigs = sysConfigMapper.selectByParams(null);
        if (sysConfigs != null && !sysConfigs.isEmpty()) {
            return sysConfigs.get(0);
        }
        return null;
    }


    private void initAppConfig() {
        SysConfig sysConfig = findDefaultSysConfig();
        if (sysConfig != null) {
            AppConfig.COMPANY_NAME = sysConfig.getConfig1();
            String config2 = sysConfig.getConfig2();
            String config3 = sysConfig.getConfig3();
            AppConfig.CUST_OPEN_DAY = config2 == null ? 0 : Integer.parseInt(config2);
            AppConfig.CUST_REMIND_DAY = config3 == null ? 0 : Integer.parseInt(config3);
        }
    }
}
